<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>挑战1</title>
</head>
<body>
<script>
  //1 模拟一个用户数据源
  const users = [ { username: 'user1', password: 'password1' }, { username: 'user2', password: 'password2' }, { username: 'user3', password: 'password3' } ];
  //2 模拟登陆验证函数
  function authenticateUser(username, password) {
    return new Promise((resolve, reject) => {
      // 使用 setTimeout 模拟异步操作
      setTimeout(() => {
        const user = users.find(user => user.username === username && user.password === password);
        if (user) {
          resolve(user);
        } else {
          reject(new Error('用户名或密码错误')); } }, 1000); });
  }
  // 调用 authenticateUser 函数进行登陆验证
  authenticateUser('user1', 'password1') .then(user => {
    console.log(`Welcome, ${user.username}!`); })
          .catch(error => { console.error(error.message); });
</script>

</body>
</html>


